﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
    /// <summary>
    /// Interaction logic for DangNhap.xaml
    /// </summary>
    public partial class DangNhap : Window
    {
        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private XuLyThem m_cXuLyThem = new XuLyThem();

        public DangNhap()
        {
            InitializeComponent();
        }

        private bool KiemTraNhap()
        {
            if (txtID.Text == "" || txtMK.Password == "")
                return true;
            else return false;
        }

        private void bntThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private bool KiemTraMatKhau()
        {
            string admin = "admin";
            if (txtMK.Password.Equals(admin) && txtID.Text.Equals(admin))
                return true;
           // else
            //{
            if (m_cXuLyThem.LoaiNguoiDung(txtID.Text) == "DG")
            {
                if (serv.SelectMatKhau(txtID.Text).ToString() != "-1") 
                    if (txtMK.Password == serv.SelectMatKhau(txtID.Text).ToString())
                        return true;
                    else return false;
                else return false;
            }
            if (m_cXuLyThem.LoaiNguoiDung(txtID.Text) == "NV")
            {
                if (serv.SelectMatKhau_NhanVien(txtID.Text).ToString() != "-1")
                    if (txtMK.Password == serv.SelectMatKhau_NhanVien(txtID.Text).ToString())
                        return true;
                    else return false;
                else return false;
            }
            //}
            return false;
        }

        private void bntDN_Click(object sender, RoutedEventArgs e)
        { 
            if (KiemTraNhap())
                MessageBox.Show("Phải nhập đầy đủ thông tin trước khi đăng nhập", "Thông báo");
            else
            {
                if (KiemTraMatKhau())
                {
                    if (txtMK.Password == "admin" && txtID.Text == "admin")
                    {
                        MainWindow m_fMainWindow = new MainWindow(txtID.Text);
                        m_fMainWindow.Show();
                        this.Close();
                    }
                    else
                    {
                        if (m_cXuLyThem.LoaiNguoiDung(txtID.Text) == "DG")
                        {
                            if (serv.SelectDoiMatKhau(txtID.Text) == 0)
                            {
                                DoiMatKhau m_fDoiMatKhau = new DoiMatKhau(txtID.Text);
                                m_fDoiMatKhau.Show();
                                this.Close();
                            }
                            else
                            {
                                MainWindow m_fMainWindow = new MainWindow(txtID.Text);
                                m_fMainWindow.Show();
                                this.Close();
                            }
                        }

                        if (m_cXuLyThem.LoaiNguoiDung(txtID.Text) == "NV")
                        {
                            if (serv.SelectDoiMatKhau_NhanVien(txtID.Text) == 0)
                            {
                                DoiMatKhau m_fDoiMatKhau = new DoiMatKhau(txtID.Text);
                                m_fDoiMatKhau.Show();
                                this.Close();
                            }
                            else
                            {
                                MainWindow m_fMainWindow = new MainWindow(txtID.Text);
                                m_fMainWindow.Show();
                                this.Close();
                            }
                        }
                    }
                }
                else MessageBox.Show("Sai thông tin! Không đăng nhập được!", "Thông Báo");
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                bntDN_Click(sender, e);
        }

        private void txtMK_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                bntDN_Click(sender, e);
        }
    }
}
